# -*- coding: utf-8 -*-
from odoo import models, fields

class ProductPriceImport(models.Model):
    _inherit = 'product.template'

    supply_price = fields.Float(string='匯入的供應商價格')
    type = fields.Selection(default='product')
    categ_id = fields.Many2one(default=lambda self: self.env['product.category'].search([('name','=',u'可銷售')]))
    purchase_ok = fields.Boolean(default=False)

    def remove_product_tax(self):
        sql = 'DELETE FROM product_taxes_rel' # 移除所有產品的銷項稅
        self._cr.execute(sql)
        # sql = 'UPDATE product_template SET type = %s WHERE type = %s' % ('consu', 'product') # 將產品類型從庫存商品轉為消耗品
        # self._cr.execute(sql)

class ProductPricelistInherit(models.Model):
    _inherit = 'product.pricelist'

    def import_price_from_supply_price(self):
        product = self.env['product.product'].search([('attribute_value_ids', '=', False), ('sale_ok', '=', True),
                                                      ('type', '=', 'product')])

        res = []
        for line in product:
            is_update = False
            for row in self.item_ids:
                if row.product_id == line:
                    is_update = True
                    update_dict = {
                        'fixed_price': line.supply_price
                    }
                    res.append([1, row.id, update_dict])
                    break

            if is_update is False:
                insert_dict = {
                    'applied_on': '0_product_variant',
                    'product_id': line.id,
                    'fixed_price': line.supply_price
                }
                res.append([0, 0, insert_dict])

        self.write({
            'item_ids': res
        })